module Main where

import System.Console.GetOpt
import Data.Maybe ( fromMaybe )
import System.Environment.UTF8 (getArgs)
import System.Exit (exitWith)
import System.IO (stderr, hPutStrLn, stderr, stdout, getLine)
import GHC.IOBase (ExitCode(..))

{-  Command-line syntax:

  sld [options] <arguments>

    where options are:
  -u, --unzip         -- source is/are filename(s) which are <arguments>
  -a, --extractAll    -- extract all the information
  -l, --extractLandscape -- extract landscape
  -c, --extractCompRepository -- extract component repository
  -n, --extractNameReservation (not implemented yet)
  -i  -- extract information
  -v  -- be verbose
  -V, --version
  --help
-}

data CLOption
     = Verbose | Version | Unzip [String] |Help
       deriving (Eq, Show)

options :: [OptDescr CLOption]
options = [Option ['v'] ["verbose"] (NoArg Verbose) "be verbose",
    Option ['h'] ["help"] (NoArg Help) "help123"
    ]

parseArgs :: [String] -> IO ([CLOption], [String])
parseArgs argv =
    case getOpt Permute options argv of
        (o,n,[]  )
          | Help `elem` o -> help
          | True -> return (o,n)
        (_,_,errs) -> ioError (userError (concat errs ++ usageInfo header options))
    where
        header = "Usage: sld [OPTION...] [files...]"
        dump = hPutStrLn stderr
        help = dump info >> exitWith ExitSuccess
        info = usageInfo header options

main = do
    argv <- getArgs
    parseArgs argv

m2 = do
    parseArgs ["--help"]

{- import System.IO
import Codec.Archive.Zip
import Data.Maybe
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as B
import qualified Data.ByteString.Lazy.Internal as BI
import qualified Data.ByteString.Lazy.Char8 as B8
import qualified Data.ByteString.Lazy.UTF8 as BU8
import Control.Applicative

import Text.XML.Light.Input
import Text.XML.Light.Proc
import Text.XML.Light.Types

s1 = "/home/kuznetsov/svn/hixay/svn_hixay_data/X70_LD_Full_1_0.zip"
s2 = "/home/kuznetsov/svn/hixay/svn_hixay_data/NewX70.zip"
s3 = "/home/kuznetsov/svn/hixay/svn_hixay_data/1.zip"
s4 = "/home/kuznetsov/svn/hixay/svn_hixay_data/LD/export01.xml"

-- makes archive in runtime from file object
getArchive :: String -> IO Archive
getArchive path = toArchive <$> B.readFile path

export = "export"

onlyExports :: [FilePath] -> [FilePath]
--onlyExports = filter(\x-> take (length export) x == export)
onlyExports = filter ((=="cimdata"))
--onlyExports = id

getEntries :: Archive -> [String]
getEntries a = map (getEntry a) (onlyExports $ filesInArchive a)

getEntries2 :: [String] -> [[Content]]
getEntries2 l = map parseXML l

getEntry :: Archive -> FilePath -> String
getEntry a n = case e of
    Just x -> BU8.toString $ fromEntry x
    Nothing -> ""
  where
    e = findEntryByPath n a

main = do
  putStrLn s2
  -- b <- mapM getEntries2 ( getEntries $ getArchive s2)
  print "aa"
  --putStrLn $ show $ filesInArchive a  

a = B.pack [100]
b = B.pack $ replicate 70000 100
c = B.pack [120]

fr = BI.foldrChunks
fl = BI.foldlChunks

-}
